<script lang="ts" setup>
import { ref } from 'vue'
import { GetInviteData } from '@/api/modules/freetrialapply'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const route = useRoute()

const findFrom = ref({
  TrialApplyId: route.query.TrialApplyId,
  UserId: '',
  NickName: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.TrialApplyId && ({ TrialApplyId: findFrom.value.TrialApplyId })),
    ...(findFrom.value.UserId !== '' && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.NickName !== '' && ({ NickName: findFrom.value.NickName })),
  }
  await GetInviteData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 添加
function AddData() {
  router.push({
    name: 'freetrialApply',
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
}

onMounted(() => {
  GetDataList()
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="昵称">
                <el-input v-model="findFrom.NickName" placeholder="请输入昵称" @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button @click="AddData">
          返回申请记录
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="trialApplyId" label="试用品申请ID" width="200" />
        <el-table-column align="center" prop="subscriberInfo.userId" label="用户ID" width="200" />
        <el-table-column align="center" prop="subscriberInfo.scOpenId" label="OpenID" width="200" />
        <el-table-column align="center" prop="subscriberInfo" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriberInfo?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriberInfo?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriberInfo?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nickName" label="昵称" width="200" />
        <el-table-column align="center" label="头像" width="200">
          <template #default="scope">
            <ElImage style=" width: auto;height: 100px;" :src="imgDomain + scope.row.avatar" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="recStatus" label="状态" width="200">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.recStatus === 1 ? '已助力' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="addTime" label="添加时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="修改时间" width="200" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
